<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreSessionMemcacheBase extends CoreSessionBase {
	var $memcache;
	
	//constructor
	function CoreSessionMemcacheBase() {
		parent::CoreSessionBase();
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'lib.memcache.class.php');
		$this->memcache = new LibMemcache();
	}
	
	//methods
	function _sess_open($save_path, $session_name) {
		return true;
	}
	
	function _sess_close() {
		return true;
	}
	
	function _sess_read($key) {
		$key = 'sesskey' . $key;
		return $this->memcache->get($key, 'value');
		//
		$query = "select value from pp_sessions where sesskey = '" . func_db_input($key) . "' and expiry > '" . time() . "'";
		$row = $this->adb->getRow($query);
		if( $row['value'] ) {
			return $row['value'];
		}
		return false;
	}
	
	function _sess_write($key, $val) {
		return $this->memcache->set($key, $val);
		//
		if( is_object($this->adb) ) {
			//
		} else {
			$this->adb = ADODBDatabase::getInstance();
			$this->adb->connect();
			$this->adb->query("SET NAMES 'utf8'");
		}
		
		$expiry = time() + CONST_SESSION_LIFE_TIME;
		$value = $val;
		
		$query = "select count(*) as total from pp_sessions where sesskey = '" . func_db_input($key) . "'";
		$row = $this->adb->getRow($query);
		
		if( $row['total'] > 0 ) {
			$this->adb->query("update pp_sessions set expiry = '" . func_db_input($expiry) . "', value = '" . func_db_input($value) . "' where sesskey = '" . func_db_input($key) . "'");
			return true;
		} else {
			$this->adb->query("insert into pp_sessions values ('" . func_db_input($key) . "', '" . func_db_input($expiry) . "', '" . func_db_input($value) . "')");
			return true;
		}
	}
	
	function _sess_destroy($key) {
		$this->adb->query("delete from pp_sessions where sesskey = '" . func_db_input($key) . "'");
		return true;
	}
	
	function _sess_gc($maxlifetime) {
		$this->adb->query("delete from pp_sessions where expiry < '" . time() . "'");
		
		return true;
	}
}
//
?>